﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ShippingID.Controllers
{
    public class PercursoController : Controller
    {
        //
        // GET: /Percurso/

        public ActionResult Index()
        {
            ViewData["Cliente"] = new BL.ServiceGeral().RetornarTodos<Modelo.Cliente>();
            return View(new BL.ServiceGeral().RetornarTodos<Modelo.Percurso>());
        }

        //
        // GET: /Percurso/Details/5

        public ActionResult Details(int id)
        {
            CargaSelectListCliente();
            return View(new BL.ServiceGeral().RetornarPorID<Modelo.Percurso>(id));
        }

        //
        // GET: /Percurso/Create

        public void CargaSelectListCliente()
        {
            List<SelectListItem> cliente = new List<SelectListItem>();
            foreach (Modelo.Cliente item in new BL.ServiceGeral().RetornarTodos<Modelo.Cliente>())
            {
                cliente.Add(new SelectListItem { Text = item.Nome, Value = item.ClienteID.ToString() });
            }
            ViewData["Clientes"] = cliente;
        }
        public ActionResult Create()
        {

            CargaSelectListCliente();
            return View();
        } 

        //
        // POST: /Percurso/Create

        private Modelo.Percurso retornaPercurso(FormCollection collection)
        {
          return  (
              
                new Modelo.Percurso(
                Convert.ToInt32(collection["Clientes"]), 
                Convert.ToInt32(collection["DistanciaTotal"]),
                Convert.ToDecimal(collection["TempoEsperado"]),
                collection["Nome"].ToString(),
                (collection["ID"]!=null)?Convert.ToInt32(collection["ID"].ToString()):-1 
                
                )
                );
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if( new BL.ServiceGeral().Gravar<Modelo.Percurso>(retornaPercurso(collection)))
            {
                return RedirectToAction("Index");
            }
            else
            {
                CargaSelectListCliente();
                return View();
            }
        }
        
        //
        // GET: /Percurso/Edit/5
 
        public ActionResult Edit(int id)
        {
            CargaSelectListCliente();
            return View(new BL.ServiceGeral().RetornarPorID<Modelo.Percurso>(id) );
        }

        //
        // POST: /Percurso/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {   
                if( new BL.ServiceGeral().Alterar<Modelo.Percurso>(retornaPercurso(collection )))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    CargaSelectListCliente();
                    return View();
                }
        }

        //
        // GET: /Percurso/Delete/5
 
        public ActionResult Delete(int id)
        {
            CargaSelectListCliente();
            return View(new BL.ServiceGeral().RetornarPorID<Modelo.Percurso>(id));
        }

        //
        // POST: /Percurso/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if(new BL.ServiceGeral().Excluir<Modelo.Percurso>(id))
            {
                return RedirectToAction("Index");
            }
            else
            {   
                return View();
            }
        }





    }
}
